﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows;

using SBPweb.RIA.Shared.Contracts.Events;

using SBPweb.PROvibe.Shared.Library.Components;
using SBPweb.PROvibe.Shared.Library.Layout;
using SBPweb.PROvibe.Client.Library.Modularity;

namespace SBPweb.PROvibe.Client.Modules.MOSS14Connector.Modularity
{

    public class Moss14ConnectorModule : ClientModuleBase
    {

        #region Fields

        private Services.Moss14ConnectorService service;

        #endregion

        #region Constructor logic

        public Moss14ConnectorModule()
            : base()
        {
        }

        public Moss14ConnectorModule(IModule module)
            : base(module)
        {
            service = new Services.Moss14ConnectorService(module);
            service.Initialized += new EventHandler(service_Initialized);
            ClientContext.ContentContextController.RegisterService<Services.Moss14ConnectorService>(service);
        }

        #endregion

        #region Properties

        #endregion

        #region Methods

        protected override void OnInitialize()
        {
            service.Initialize();
            //MessageBox.Show(service.MossContext.Web.Title);
            //controls.ManagableItems.Add(new Controls.ToolBar());
        }

        protected override void OnTerminate()
        {
            //controls.ManagableItems.Clear();
        }

        private void service_Initialized(object sender, EventArgs e)
        {
            ClientContext.Dispatcher.BeginInvoke(() =>
            {
                
            //    //string path = string.Format("{0}{1}_layouts/{2}",
            //    //    service.MossContext.Site.Url,
            //    //    service.MossContext.Web.ServerRelativeUrl,
            //    //    "AddGallery.xap");
            //    //ClientContext.PackageLoaderController.LoadFromPackage<Application>(path, "Microsoft.SharePoint.Solutions.AddGallery.UI.App", new AsyncRequestHandler<Application>(s));
            //    //ClientContext.PackageLoaderController.LoadPackage(path, new AsyncRequestArgs<IList<Assembly>>(new AsyncRequestHandler<IList<Assembly>>(s)));
            //    //MessageBox.Show(string.Format("MOSS v{0}\n{1}\n{2}", service.MossContext.ServerVersion.ToString(), service.MossContext.Url, service.MossContext.Web.Title));
            });
        }

        //private void s(object sender, GenericEventArgs<Application> e)
        //{
        //}

        private void s(object sender, GenericEventArgs<IList<Assembly>> e)
        {
            //Assembly asm = e.Result.FirstOrDefault();
            //Type t = asm.GetType("Microsoft.SharePoint.Solutions.AddGallery.UI.App");
            //try
            //{
            //    var app = Activator.CreateInstance(t);
            //}
            //catch
            //{
            //}
        }

        #endregion

    }

}